﻿using DocumentFormat.OpenXml.Lite.Helper;
using DocumentFormat.OpenXml.Lite.Xml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace DocumentFormat.OpenXml.Lite.WordprocessingML.Word.RunElements
{
    [XmlRoot("sym", Namespace = Constants.WordMain)]
    public class SymbolCharacter : XmlSerializable, IRunSingleElement
    {
        [Optional]
        [XmlAttribute("font", Namespace = Constants.WordMain, Form = XmlSchemaForm.Qualified)]
        public string Font { get; set; }

        /// <summary>
        /// Character code with hexadecimal number. Length: 2
        /// </summary>
        [XmlAttribute("char", Namespace = Constants.WordMain, Form = XmlSchemaForm.Qualified)]
        public string Code 
        {
            get { return code; }
            set 
            {
                if (value == null || value.Length != 2 || !Helpers.ParseHexadecimalValue(value))
                {
                    throw new ArgumentException("String: " + value +
                        "is not a valid hexadecimal string or length is not 2!");
                }
                code = value;
            }
        }

        [XmlIgnore]
        private string code;

        public SymbolCharacter() { }

        public void Copy(object obj)
        {
            if (obj is SymbolCharacter)
            {
                this.Font = (obj as SymbolCharacter).Font;
                this.Code = (obj as SymbolCharacter).Code;
            }
        }
    }
}
